<?php

/**
 * This is the model class for table "prefix_comment".
 *
 * The followings are the available columns in table 'prefix_comment':
 * @property string $comment_id
 * @property string $comment_pid
 * @property string $target_id
 * @property string $target_type
 * @property integer $target_parent_id
 * @property string $user_id
 * @property string $comment_text
 * @property string $comment_text_hash
 * @property string $comment_date
 * @property string $comment_user_ip
 * @property double $comment_rating
 * @property string $comment_count_vote
 * @property integer $comment_delete
 * @property integer $comment_publish
 * @property Comment $parentComment
 * @property Comment[] $comments
 * @property User $user
 * @property CommentOnline[] $onlineComments
 */
class Comment extends LsActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Comment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return Config::Get('db.table.comment');
	}
	
	public function getEntity(){
		if($entity = parent::getEntity()){
			
			if($this->hasRelated('userFavourite')
			&& $favourite = $this->getRelated('userFavourite')){
				$entity->setIsFavourite((bool) $favourite);
			}
			
			if($this->hasRelated($this->target_type)
			&& $target = $this->getRelated($this->target_type)){
				$entity->setTarget($target->getEntity());
			}
			
			return $entity;
		}
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, comment_text, comment_text_hash, comment_date, comment_user_ip', 'required'),
			array('target_parent_id, comment_delete, comment_publish', 'numerical', 'integerOnly'=>true),
			array('comment_rating', 'numerical'),
			array('comment_pid, target_id, user_id, comment_count_vote', 'length', 'max'=>11),
			array('target_type', 'length', 'max'=>5),
			array('comment_text_hash', 'length', 'max'=>32),
			array('comment_user_ip', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('comment_id, comment_pid, target_id, target_type, target_parent_id, user_id, comment_text, comment_text_hash, comment_date, comment_user_ip, comment_rating, comment_count_vote, comment_delete, comment_publish', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'parentComment' => array(self::BELONGS_TO, 'Comment', 'comment_pid'),
			'comments' => array(self::HAS_MANY, 'Comment', 'comment_pid'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'onlineComment' => array(self::HAS_ONE, 'CommentOnline', 'comment_id'),
			'countFavourites' => array(
				self::STAT,
				'Favourite',
				'target_id',
				'condition' => '{%child}.target_type="comment"',
			),
			'favourites' => array(
				self::HAS_MANY,
				'Favourite',
				'target_id',
				'condition' => '{%child}.target_type="comment"',
			),
			'topic' => array(
				self::BELONGS_TO,
				'Topic',
				'target_id',
			),
			'talk' => array(
				self::BELONGS_TO,
				'Talk',
				'target_id',
			),
			'userFavourite' => array(
				self::HAS_ONE,
				'Favourite',
				'target_id',
				'on' => '{%child}.target_type="comment"'
					.' and {%child}.user_id='.(User::me()?User::me()->user_id:0),
			),
			'countVotes' => array(
				self::STAT,
				'Vote',
				'target_id',
				'condition' => '{%child}.target_type="comment"',
			),
			'votes' => array(
				self::HAS_MANY,
				'Vote',
				'target_id',
				'condition' => '{%child}.target_type="comment"',
			),
			'vote' => array(
				self::HAS_ONE,
				'Vote',
				'target_id',
				'condition' => '',
				'on' => '{%child}.target_type="comment"'
					.' and {%child}.user_voter_id='.(User::me()?User::me()->user_id:0),
			),
		);
	}
	
	public function lastByTime($time=86400){
		if(preg_match('#^(\d+)([hdmy])$#', $time, $match)){
			if($match[2]=='h'){
				$time = $match[1]*3600;
			}elseif($match[2]=='d'){
				$time = $match[1]*3600*24;
			}elseif($match[2]=='m'){
				$time = $match[1]*2629744;
			}elseif($match[2]=='y'){
				$time = $match[1]*31556926;
			}
		}
		$this->getDbCriteria()->mergeWith(array(
			'condition' => '{%this}.comment_date>="'
				.date("Y-m-d H:00:00",time()-$time).'"'
		));
		return $this;
	}
	

	
	public function defScope(){
		return array(
		//	'condition' => '{%this}.comment_delete=0',
		);
	}
	
	
	public function scopes(){
		return array(
			'toTopic' => array(
				'condition' => '{%this}.target_type="topic"',
				'alias' => $this->tableName(),
			),
			'toTalk' => array(
				'condition' => '{%this}.target_type="talk"',
				'alias' => $this->tableName(),
			),
			'visible' => array(
				'condition' => '{%this}.comment_delete=0',
				'alias' => $this->tableName(),
			),
		);
	}
	
	
	public function getUrl(){
		if(!$this->getMetaData()->hasRelation($this->target_type)
		|| !($target = $this->{$this->target_type})){
			return;
		}
		return $target->getEntity()->getUrl().'#comment'.$this->comment_id;
	}

	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'comment_id' => 'Comment',
			'comment_pid' => 'Comment Pid',
			'target_id' => 'Target',
			'target_type' => 'Target Type',
			'target_parent_id' => 'Target Parent',
			'user_id' => 'User',
			'comment_text' => 'Comment Text',
			'comment_text_hash' => 'Comment Text Hash',
			'comment_date' => 'Comment Date',
			'comment_user_ip' => 'Comment User Ip',
			'comment_rating' => 'Comment Rating',
			'comment_count_vote' => 'Comment Count Vote',
			'comment_delete' => 'Comment Delete',
			'comment_publish' => 'Comment Publish',
		);
	}
}